library(dplyr)
#library(psych) #for pairs.panels, but could use other packages, e.g. GGalley
library(lavaan)
library(semPlot)
library(DiagrammeR)
library(ggplot2)
library(tidyr)

Import data

combined=read.csv("data/monthly_averages/monthly_data_compiled_regions.csv",stringsAsFactors = F)
cnames=read.csv("analysis/column_names_region_monthly.csv", stringsAsFactors = F)
dsub=filter(combined, Year>=1995) %>% arrange(Region,Year,Month)
focaldata=dsub[,cnames$Datacolumn]
fvars=cnames$Shortname
colnames(focaldata)=fvars
regions=unique(focaldata$region)
regionorder=c("Far West","West","North","South")
focaldata=focaldata%>% 
  mutate(decyear=year+(month-1)/12,
         sinmon=sin(2*pi/12*month),
         cosmon=cos(2*pi/12*month))

#focal variables
varnames=c("temp", "flow","nitrate","ammonia","dophos","chla","hcope","clad","amphi","pcope","mysid","potam","corbic","sside","estfish_bsot","estfish_bsmt")

#labels for lagged vars
cnameslag=cnames
cnameslag$Shortname=paste0(cnameslag$Shortname,"_1")
cnameslag$Diagramname=paste(cnameslag$Diagramname,"(t-1)")
cnameslag=rbind(cnames,cnameslag)

source("analysis/myLavaanPlot.r")

Notes:
Determine which vars are appropriate for which regions and at what lags.
Write out path diagram for each region.
Model with and without fish, probably, because of missing data.

Data prep

Log transform, scale.
Within and across regions.
Create set with regional monthly means removed.

#log transform
logvars=fvars[cnames$Log=="yes"]
logtrans=function(x) {
  x2=x[which(!is.na(x))]
  if(any(x2==0)) {log(x+min(x2[which(x2>0)],na.rm=T))}
  else {log(x)}
}
focaldatalog = focaldata %>% 
  mutate_at(logvars,logtrans)

#scale data
fdr0=focaldatalog
tvars=fvars[-(1:3)]

#scaled within regions
fdr=fdr0 %>% 
  group_by(region) %>% 
  #scale
  mutate_at(tvars,scale) %>% 
  #lag
  mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
  ungroup() %>% 
  as.data.frame()

#scaled within regions, remove monthly means
fdr_ds=fdr %>% 
  group_by(region,month) %>%
  mutate_at(tvars,list("mm"=function(x) {mean(x,na.rm = T)})) %>% 
  mutate_at(tvars,function(x) {x-mean(x,na.rm = T)}) %>% 
  ungroup() %>% 
  #lag
  group_by(region) %>% 
  mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
  ungroup() %>% 
  as.data.frame()

#scaled across regions
fdr1=fdr0 %>% 
  #scale
  mutate_at(tvars,scale) %>% 
  #lag
  group_by(region) %>% 
  mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
  ungroup() %>% 
  as.data.frame()

#scaled across regions, monthly means removed
fdr1_ds=fdr1 %>% 
  group_by(region,month) %>%
  mutate_at(tvars,list("mm"=function(x) {mean(x,na.rm = T)})) %>% 
  mutate_at(tvars,function(x) {x-mean(x,na.rm = T)}) %>% 
  ungroup() %>% 
  #lag
  group_by(region) %>% 
  mutate_at(tvars,list("1"=lag,"2"=function(x) {lag(x,2)})) %>% 
  ungroup() %>% 
  as.data.frame()

Data availability by series and region (prop zeros and prop missing)

FW: ignore clad, corbic, sside
W: ignore corbic, sside
S: ignore potam

dataavail=focaldata %>% 
  gather(var, value, 4:length(fvars)) %>% 
  group_by(region, var) %>% 
  summarize(
    propmissing=length(which(is.na(value)))/length(value),
    propzeros=length(which(value==0))/length(which(!is.na(value)))) %>% 
  as.data.frame()

#these variables should not be used (too many zeros)
filter(dataavail,propzeros>0.5 | is.na(propzeros))
##      region          var propmissing propzeros
## 1  Far West         clad   0.1382637 0.9514925
## 2  Far West       corbic   0.1382637 1.0000000
## 3  Far West marfish_bsot   0.4565916 0.6508876
## 4  Far West   sbass_bsmt   0.4951768 0.7515924
## 5  Far West   sbass_bsot   0.4565916 0.6982249
## 6  Far West   smelt_bsmt   0.4951768 0.9808917
## 7  Far West   smelt_bsot   0.4565916 0.9940828
## 8  Far West        sside   1.0000000       NaN
## 9     North    lfin_bsmt   0.5144695 0.8741722
## 10    North    lfin_bsot   0.4694534 0.9393939
## 11    North marfish_bsmt   0.5144695 0.9735099
## 12    North marfish_bsot   0.4694534 0.9939394
## 13    North   smelt_bsot   0.4694534 0.8242424
## 14    South    lfin_bsmt   0.5128205 1.0000000
## 15    South    lfin_bsot   0.4679487 1.0000000
## 16    South marfish_bsmt   0.5128205 1.0000000
## 17    South marfish_bsot   0.4679487 1.0000000
## 18    South        potam   0.1378205 0.9702602
## 19    South   sbass_bsmt   0.5128205 0.6118421
## 20    South   smelt_bsmt   0.5128205 0.8486842
## 21    South   smelt_bsot   0.4679487 0.9156627
## 22     West       corbic   0.1350482 0.8066914
## 23     West marfish_bsot   0.4598071 0.9047619
## 24     West   smelt_bsmt   0.4983923 0.5128205
## 25     West   smelt_bsot   0.4598071 0.7619048
## 26     West        sside   1.0000000       NaN
#these variables have lots of missing data
filter(dataavail,propmissing>0.45 | is.na(propmissing))
##      region          var propmissing   propzeros
## 1  Far West estfish_bsmt   0.4951768 0.165605096
## 2  Far West estfish_bsot   0.4565916 0.201183432
## 3  Far West    lfin_bsmt   0.4951768 0.356687898
## 4  Far West    lfin_bsot   0.4565916 0.248520710
## 5  Far West marfish_bsmt   0.4951768 0.006369427
## 6  Far West marfish_bsot   0.4565916 0.650887574
## 7  Far West   sbass_bsmt   0.4951768 0.751592357
## 8  Far West   sbass_bsot   0.4565916 0.698224852
## 9  Far West   smelt_bsmt   0.4951768 0.980891720
## 10 Far West   smelt_bsot   0.4565916 0.994082840
## 11 Far West        sside   1.0000000         NaN
## 12    North estfish_bsmt   0.5176849 0.126666667
## 13    North estfish_bsot   0.4694534 0.109090909
## 14    North    lfin_bsmt   0.5144695 0.874172185
## 15    North    lfin_bsot   0.4694534 0.939393939
## 16    North marfish_bsmt   0.5144695 0.973509934
## 17    North marfish_bsot   0.4694534 0.993939394
## 18    North   sbass_bsmt   0.5176849 0.480000000
## 19    North   sbass_bsot   0.4694534 0.133333333
## 20    North   smelt_bsmt   0.5144695 0.403973510
## 21    North   smelt_bsot   0.4694534 0.824242424
## 22    South estfish_bsmt   0.5128205 0.177631579
## 23    South estfish_bsot   0.4679487 0.006024096
## 24    South    lfin_bsmt   0.5128205 1.000000000
## 25    South    lfin_bsot   0.4679487 1.000000000
## 26    South marfish_bsmt   0.5128205 1.000000000
## 27    South marfish_bsot   0.4679487 1.000000000
## 28    South   sbass_bsmt   0.5128205 0.611842105
## 29    South   sbass_bsot   0.4679487 0.012048193
## 30    South   smelt_bsmt   0.5128205 0.848684211
## 31    South   smelt_bsot   0.4679487 0.915662651
## 32     West estfish_bsmt   0.4983923 0.019230769
## 33     West estfish_bsot   0.4598071 0.017857143
## 34     West    lfin_bsmt   0.4983923 0.250000000
## 35     West    lfin_bsot   0.4598071 0.392857143
## 36     West marfish_bsmt   0.4983923 0.115384615
## 37     West marfish_bsot   0.4598071 0.904761905
## 38     West   sbass_bsmt   0.4983923 0.141025641
## 39     West   sbass_bsot   0.4598071 0.035714286
## 40     West   smelt_bsmt   0.4983923 0.512820513
## 41     West   smelt_bsot   0.4598071 0.761904762
## 42     West        sside   1.0000000         NaN

Time series plots

Cross-correlation matrices

(only sig correlations shown… no correction for multiple comparisons)

Other notes:

Detrended fish indices are NOT correlated in S!

Nitrate and ammonia are positively correlated, max at lag 0 all regions.
Nitrate and dophos are positively correlated, max at lag 0 all regions.
Ammonia and dophos are positively correlated, lag 0 for FW and S, ammonia lags dphos by 3 months in W and N.

Chla nitrate neg correlated, lag 0.
Chla ammonia neg correlated, lag 0.
Chla dophos relationship unclear.

High flow 2-4 month prev = high chla

Hcope lags chla by 1, positive, except FW.
Clad seem to precede chla by 2, positive.
Amphi relationship unclear, prob bc not eating chla in water column.
In N and W, chla lags potam, negative. The opposite in W.

Mysid and hcope postive, lag 0.
In S and W, hcope lags pcope, negative.

Exploratory plots

SEM model

Note: You get somewhat different answers if you use a multigroup model vs. fit each region individually. I am not entirely sure why, but I think there is some data pooling going on like in hierarchical models. I would have to look into the math. I know that if you use the multigroup framework you can impose constraints like requiring certain parameters to be the same across groups (see lavaan manual). Obviously, if data on a variable you want to use are missing from one site, you can’t use the multigroup option (you will get an error) and have to fit individually.

I haven’t looked in to how to modify the new (nicer) plotting code to work with the lags and multiple groups, but that is something that can be done.

I haven’t added corbic and sside, but that is something to explore.

Chla, nutrients, flow

Clams don’t seem to have any impact on chla. Dropping dophos because strongly correlated with ammonia.

# mod1='chla~ammonia+dophos+chla_1+flow_1
#       ammonia~flow_1+chla_1
#       dophos~flow_1
#       ammonia~~dophos'

mod1='chla~ammonia+chla_1+flow_1
      ammonia~flow_1+chla_1'

#you can either do a multigroup model by region
# modfit1=sem(mod1, data=fdr_ds, group = "region")
# summary(modfit1, standardized=T, rsq=T)
# 
# par(mfrow=c(2,2))
# semPaths(modfit1, "std", edge.label.cex = 1, residuals = F,  node.width=3, nCharNodes = 0, intercepts = F, title = T)
# #residuals(modfit1)

#or fit separately for each region 
modfitFW=sem(mod1, data=filter(fdr_ds,region=="Far West"))
modfitW=sem(mod1, data=filter(fdr_ds,region=="West"))
modfitN=sem(mod1, data=filter(fdr_ds,region=="North"))
modfitS=sem(mod1, data=filter(fdr_ds,region=="South"))
summary(modfitFW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 13 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                          7
## 
##                                                   Used       Total
##   Number of observations                           297         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     ammonia          -0.301    0.050   -5.982    0.000   -0.301   -0.315
##     chla_1            0.314    0.052    6.028    0.000    0.314    0.317
##     flow_1           -0.086    0.054   -1.605    0.108   -0.086   -0.085
##   ammonia ~                                                             
##     flow_1           -0.165    0.061   -2.690    0.007   -0.165   -0.156
##     chla_1            0.022    0.060    0.363    0.717    0.022    0.021
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.571    0.047   12.186    0.000    0.571    0.803
##    .ammonia           0.762    0.063   12.186    0.000    0.762    0.974
## 
## R-Square:
##                    Estimate
##     chla              0.197
##     ammonia           0.026
summary(modfitW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 16 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                          7
## 
##                                                   Used       Total
##   Number of observations                           304         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       0.000
##   Degrees of freedom                                 0
##   Minimum Function Value               0.0000000000000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     ammonia          -0.194    0.057   -3.408    0.001   -0.194   -0.206
##     chla_1            0.340    0.053    6.383    0.000    0.340    0.341
##     flow_1           -0.002    0.051   -0.033    0.974   -0.002   -0.002
##   ammonia ~                                                             
##     flow_1           -0.426    0.045   -9.407    0.000   -0.426   -0.474
##     chla_1            0.110    0.053    2.064    0.039    0.110    0.104
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.422    0.034   12.329    0.000    0.422    0.853
##    .ammonia           0.427    0.035   12.329    0.000    0.427    0.770
## 
## R-Square:
##                    Estimate
##     chla              0.147
##     ammonia           0.230
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 14 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                          7
## 
##                                                   Used       Total
##   Number of observations                           304         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     ammonia          -0.235    0.061   -3.846    0.000   -0.235   -0.222
##     chla_1            0.218    0.055    3.971    0.000    0.218    0.218
##     flow_1           -0.051    0.056   -0.901    0.368   -0.051   -0.052
##   ammonia ~                                                             
##     flow_1           -0.293    0.050   -5.840    0.000   -0.293   -0.317
##     chla_1            0.047    0.051    0.915    0.360    0.047    0.050
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.594    0.048   12.329    0.000    0.594    0.912
##    .ammonia           0.524    0.043   12.329    0.000    0.524    0.897
## 
## R-Square:
##                    Estimate
##     chla              0.088
##     ammonia           0.103
summary(modfitS, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 14 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                          7
## 
##                                                   Used       Total
##   Number of observations                           303         312
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     ammonia          -0.315    0.055   -5.697    0.000   -0.315   -0.282
##     chla_1            0.397    0.050    7.867    0.000    0.397    0.399
##     flow_1           -0.171    0.049   -3.520    0.000   -0.171   -0.177
##   ammonia ~                                                             
##     flow_1           -0.013    0.051   -0.258    0.797   -0.013   -0.015
##     chla_1            0.108    0.052    2.071    0.038    0.108    0.121
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.484    0.039   12.309    0.000    0.484    0.732
##    .ammonia           0.523    0.042   12.309    0.000    0.523    0.984
## 
## R-Square:
##                    Estimate
##     chla              0.268
##     ammonia           0.016
labelsfarwest=createLabels(modfitFW, cnameslag)
labelswest=createLabels(modfitW, cnameslag)
labelsnorth=createLabels(modfitN, cnameslag)
labelssouth=createLabels(modfitS, cnameslag)

#FAR WEST
myLavaanPlot(model=modfitFW, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=TRUE, sig=0.05, 
                         width=c("regress","latent","covs"),
                         color=c("regress","latent","covs"))
#WEST
myLavaanPlot(model=modfitW, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=TRUE, sig=0.05, 
                         width=c("regress","latent","covs"),
                         color=c("regress","latent","covs"))
#NORTH
myLavaanPlot(model=modfitN, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=TRUE, sig=0.05, 
                         width=c("regress","latent","covs"),
                         color=c("regress","latent","covs"))
#SOUTH
myLavaanPlot(model=modfitS, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=TRUE, sig=0.05, 
                         width=c("regress","latent","covs"),
                         color=c("regress","latent","covs"))
# par(mfrow=c(2,2))
# semPaths(modfitFW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("Far West", line = 1)
# semPaths(modfitW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("West", line = 1)
# semPaths(modfitN, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("North", line = 1)
# semPaths(modfitS, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("South", line = 1)

#residuals(modfitFW)

Chla, zooplankton

This definitely needs work.

modFW='chla~chla_1+hcope_1+amphi_1+flow_1
       amphi~chla_1+amphi_1+flow_1
       hcope~chla_1+hcope_1+flow_1+pcope_1
       pcope~chla_1+pcope_1+hcope_1+amphi_1+flow_1
'
modW='chla~chla_1+hcope_1+flow_1
       hcope~chla_1+hcope_1+mysid_1+pcope_1+flow_1
       pcope~chla_1+pcope_1+hcope_1+mysid_1+flow_1
       mysid~chla_1+mysid_1+hcope_1+pcope_1+flow_1
'
modN='chla~chla_1+hcope_1+amphi_1+flow_1
       amphi~chla_1+amphi_1+flow_1
       hcope~chla_1+hcope_1+flow_1+pcope_1
       pcope~chla_1+pcope_1+hcope_1+flow_1
'
modS='chla~chla_1+hcope_1+clad_1+flow_1
       clad~chla_1+clad_1+flow_1+pcope_1
       hcope~chla_1+hcope_1+flow_1+pcope_1
       pcope~chla_1+pcope_1+hcope_1+clad_1+flow_1
'
modfitFW=sem(modFW, data=filter(fdr_ds,region=="Far West"))
modfitW=sem(modW, data=filter(fdr_ds,region=="West"))
modfitN=sem(modN, data=filter(fdr_ds,region=="North"))
modfitS=sem(modS, data=filter(fdr_ds,region=="South"))
summary(modfitFW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 33 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         26
## 
##                                                   Used       Total
##   Number of observations                           233         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       6.692
##   Degrees of freedom                                 4
##   P-value (Chi-square)                           0.153
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.235    0.063    3.735    0.000    0.235    0.238
##     hcope_1           0.056    0.053    1.056    0.291    0.056    0.067
##     amphi_1           0.106    0.055    1.941    0.052    0.106    0.142
##     flow_1            0.081    0.066    1.234    0.217    0.081    0.090
##   amphi ~                                                               
##     chla_1            0.046    0.042    1.093    0.274    0.046    0.035
##     amphi_1           0.753    0.037   20.406    0.000    0.753    0.762
##     flow_1           -0.214    0.044   -4.873    0.000   -0.214   -0.181
##   hcope ~                                                               
##     chla_1            0.055    0.081    0.681    0.496    0.055    0.041
##     hcope_1           0.455    0.069    6.562    0.000    0.455    0.395
##     flow_1           -0.171    0.077   -2.233    0.026   -0.171   -0.139
##     pcope_1           0.163    0.085    1.923    0.054    0.163    0.119
##   pcope ~                                                               
##     chla_1           -0.001    0.056   -0.025    0.980   -0.001   -0.001
##     pcope_1           0.423    0.058    7.283    0.000    0.423    0.432
##     hcope_1          -0.101    0.047   -2.140    0.032   -0.101   -0.123
##     amphi_1          -0.114    0.049   -2.338    0.019   -0.114   -0.154
##     flow_1           -0.033    0.059   -0.562    0.574   -0.033   -0.038
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .amphi             0.005    0.019    0.242    0.809    0.005    0.016
##    .hcope             0.041    0.037    1.108    0.268    0.041    0.073
##    .pcope             0.005    0.025    0.215    0.830    0.005    0.014
##  .amphi ~~                                                              
##    .hcope             0.008    0.025    0.311    0.756    0.008    0.020
##    .pcope            -0.023    0.017   -1.371    0.170   -0.023   -0.090
##  .hcope ~~                                                              
##    .pcope            -0.045    0.033   -1.375    0.169   -0.045   -0.090
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.433    0.040   10.794    0.000    0.433    0.913
##    .amphi             0.195    0.018   10.794    0.000    0.195    0.238
##    .hcope             0.730    0.068   10.794    0.000    0.730    0.813
##    .pcope             0.339    0.031   10.794    0.000    0.339    0.740
## 
## R-Square:
##                    Estimate
##     chla              0.087
##     amphi             0.762
##     hcope             0.187
##     pcope             0.260
summary(modfitW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 30 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         28
## 
##                                                   Used       Total
##   Number of observations                           304         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       4.033
##   Degrees of freedom                                 2
##   P-value (Chi-square)                           0.133
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.289    0.056    5.139    0.000    0.289    0.290
##     hcope_1           0.082    0.048    1.683    0.092    0.082    0.100
##     flow_1            0.110    0.049    2.253    0.024    0.110    0.129
##   hcope ~                                                               
##     chla_1            0.092    0.069    1.336    0.182    0.092    0.075
##     hcope_1           0.279    0.062    4.517    0.000    0.279    0.277
##     mysid_1           0.182    0.062    2.914    0.004    0.182    0.168
##     pcope_1          -0.068    0.064   -1.059    0.289   -0.068   -0.055
##     flow_1            0.033    0.060    0.556    0.578    0.033    0.032
##   pcope ~                                                               
##     chla_1           -0.018    0.051   -0.346    0.729   -0.018   -0.018
##     pcope_1           0.492    0.049   10.074    0.000    0.492    0.497
##     hcope_1          -0.101    0.046   -2.190    0.029   -0.101   -0.125
##     mysid_1           0.088    0.048    1.849    0.064    0.088    0.102
##     flow_1            0.092    0.045    2.064    0.039    0.092    0.110
##   mysid ~                                                               
##     chla_1            0.151    0.058    2.610    0.009    0.151    0.134
##     mysid_1           0.419    0.053    7.904    0.000    0.419    0.422
##     hcope_1           0.110    0.052    2.119    0.034    0.110    0.119
##     pcope_1           0.064    0.054    1.173    0.241    0.064    0.056
##     flow_1           -0.007    0.050   -0.137    0.891   -0.007   -0.007
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.124    0.031    4.006    0.000    0.124    0.236
##    .pcope            -0.003    0.022   -0.150    0.881   -0.003   -0.009
##    .mysid             0.071    0.026    2.779    0.005    0.071    0.161
##  .hcope ~~                                                              
##    .pcope             0.076    0.028    2.764    0.006    0.076    0.161
##    .mysid             0.151    0.032    4.732    0.000    0.151    0.282
##  .pcope ~~                                                              
##    .mysid             0.025    0.023    1.106    0.269    0.025    0.064
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.435    0.035   12.329    0.000    0.435    0.878
##    .hcope             0.640    0.052   12.329    0.000    0.640    0.844
##    .pcope             0.354    0.029   12.329    0.000    0.354    0.724
##    .mysid             0.449    0.036   12.329    0.000    0.449    0.702
## 
## R-Square:
##                    Estimate
##     chla              0.122
##     hcope             0.156
##     pcope             0.276
##     mysid             0.298
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 31 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         25
## 
##                                                   Used       Total
##   Number of observations                           254         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       4.306
##   Degrees of freedom                                 5
##   P-value (Chi-square)                           0.506
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.171    0.062    2.762    0.006    0.171    0.172
##     hcope_1          -0.061    0.105   -0.580    0.562   -0.061   -0.043
##     amphi_1           0.025    0.056    0.451    0.652    0.025    0.028
##     flow_1            0.009    0.073    0.127    0.899    0.009    0.009
##   amphi ~                                                               
##     chla_1            0.081    0.059    1.371    0.170    0.081    0.074
##     amphi_1           0.501    0.054    9.328    0.000    0.501    0.500
##     flow_1           -0.020    0.060   -0.335    0.737   -0.020   -0.018
##   hcope ~                                                               
##     chla_1            0.003    0.037    0.078    0.938    0.003    0.004
##     hcope_1           0.239    0.063    3.782    0.000    0.239    0.253
##     flow_1           -0.180    0.044   -4.096    0.000   -0.180   -0.273
##     pcope_1          -0.063    0.040   -1.581    0.114   -0.063   -0.090
##   pcope ~                                                               
##     chla_1            0.161    0.054    2.996    0.003    0.161    0.173
##     pcope_1           0.371    0.058    6.443    0.000    0.371    0.372
##     hcope_1          -0.154    0.091   -1.694    0.090   -0.154   -0.115
##     flow_1           -0.059    0.064   -0.930    0.352   -0.059   -0.063
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .amphi            -0.023    0.040   -0.580    0.562   -0.023   -0.036
##    .hcope             0.032    0.025    1.282    0.200    0.032    0.081
##    .pcope             0.007    0.036    0.199    0.842    0.007    0.013
##  .amphi ~~                                                              
##    .hcope             0.013    0.024    0.532    0.594    0.013    0.033
##    .pcope            -0.066    0.035   -1.904    0.057   -0.066   -0.120
##  .hcope ~~                                                              
##    .pcope             0.084    0.022    3.831    0.000    0.084    0.248
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.655    0.058   11.269    0.000    0.655    0.968
##    .amphi             0.612    0.054   11.269    0.000    0.612    0.745
##    .hcope             0.235    0.021   11.269    0.000    0.235    0.795
##    .pcope             0.494    0.044   11.269    0.000    0.494    0.832
## 
## R-Square:
##                    Estimate
##     chla              0.032
##     amphi             0.255
##     hcope             0.205
##     pcope             0.168
summary(modfitS, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 35 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         27
## 
##                                                   Used       Total
##   Number of observations                           304         312
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       0.567
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.904
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.328    0.055    5.912    0.000    0.328    0.329
##     hcope_1           0.089    0.089    0.999    0.318    0.089    0.057
##     clad_1            0.083    0.058    1.435    0.151    0.083    0.079
##     flow_1           -0.167    0.058   -2.891    0.004   -0.167   -0.173
##   clad ~                                                                
##     chla_1            0.035    0.051    0.695    0.487    0.035    0.037
##     clad_1            0.478    0.053    8.954    0.000    0.478    0.481
##     flow_1            0.040    0.049    0.820    0.412    0.040    0.043
##     pcope_1          -0.004    0.045   -0.094    0.925   -0.004   -0.005
##   hcope ~                                                               
##     chla_1            0.073    0.034    2.167    0.030    0.073    0.114
##     hcope_1           0.334    0.057    5.872    0.000    0.334    0.332
##     flow_1           -0.100    0.035   -2.887    0.004   -0.100   -0.161
##     pcope_1           0.044    0.031    1.395    0.163    0.044    0.070
##   pcope ~                                                               
##     chla_1            0.115    0.056    2.047    0.041    0.115    0.113
##     pcope_1           0.422    0.052    8.165    0.000    0.422    0.425
##     hcope_1          -0.041    0.093   -0.445    0.656   -0.041   -0.026
##     clad_1            0.113    0.060    1.889    0.059    0.113    0.105
##     flow_1            0.042    0.059    0.709    0.478    0.042    0.042
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .clad              0.123    0.029    4.297    0.000    0.123    0.254
##    .hcope             0.051    0.019    2.662    0.008    0.051    0.155
##    .pcope            -0.030    0.031   -0.973    0.331   -0.030   -0.056
##  .clad ~~                                                               
##    .hcope             0.051    0.018    2.852    0.004    0.051    0.166
##    .pcope             0.050    0.028    1.765    0.078    0.050    0.102
##  .hcope ~~                                                              
##    .pcope             0.037    0.019    1.890    0.059    0.037    0.109
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.528    0.043   12.329    0.000    0.528    0.801
##    .clad              0.446    0.036   12.329    0.000    0.446    0.750
##    .hcope             0.210    0.017   12.329    0.000    0.210    0.765
##    .pcope             0.539    0.044   12.329    0.000    0.539    0.776
## 
## R-Square:
##                    Estimate
##     chla              0.199
##     clad              0.250
##     hcope             0.235
##     pcope             0.224
#modificationindices(modfitW)

labelsfarwest=createLabels(modfitFW, cnameslag)
labelswest=createLabels(modfitW, cnameslag)
labelsnorth=createLabels(modfitN, cnameslag)
labelssouth=createLabels(modfitS, cnameslag)

#FAR WEST
myLavaanPlot(model=modfitFW, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#WEST
myLavaanPlot(model=modfitW, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#NORTH
myLavaanPlot(model=modfitN, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
#SOUTH
myLavaanPlot(model=modfitS, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
# par(mfrow=c(2,2))
# semPaths(modfitFW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("Far West", line = 1)
# semPaths(modfitW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("West", line = 1)
# semPaths(modfitN, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("North", line = 1)
# semPaths(modfitS, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("South", line = 1)

Zooplankton, fish

Also needs work. Note that because fish have more missing data, more rows get dropped (compared ‘used’ and ‘total’ number of observations to previous summaries).

If you get a warning about variances being negative, that is definitely a problem. It seems to happen when you try to create a latent variables, but the variables that go into it don’t acutally have a common trend.

modFW='fish=~estfish_bsmt+estfish_bsot
       fish~hcope_1+amphi_1+mysid_1+flow_1
'
modWN='fish=~estfish_bsmt+estfish_bsot
       fish~hcope_1+amphi_1+clad_1+mysid_1+flow_1
'
modS='#fish=~estfish_bsmt+estfish_bsot
       estfish_bsmt~hcope_1+amphi_1+clad_1+mysid_1+flow_1
       estfish_bsot~hcope_1+amphi_1+clad_1+mysid_1+flow_1
'
modfitFW=sem(modFW, data=filter(fdr_ds,region=="Far West"))
modfitW=sem(modWN, data=filter(fdr_ds,region=="West"))
modfitN=sem(modWN, data=filter(fdr_ds,region=="North"))
modfitS=sem(modS, data=filter(fdr_ds,region=="South"))
summary(modfitFW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 21 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                          8
## 
##                                                   Used       Total
##   Number of observations                           125         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       8.555
##   Degrees of freedom                                 3
##   P-value (Chi-square)                           0.036
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish_bsmt      1.000                               0.507    0.556
##     estfish_bsot      1.149    0.304    3.774    0.000    0.583    0.607
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish ~                                                                
##     hcope_1          -0.168    0.059   -2.823    0.005   -0.330   -0.361
##     amphi_1          -0.130    0.074   -1.753    0.080   -0.257   -0.269
##     mysid_1          -0.104    0.079   -1.312    0.189   -0.204   -0.167
##     flow_1            0.244    0.109    2.233    0.026    0.480    0.326
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish_bsmt      0.575    0.099    5.784    0.000    0.575    0.691
##    .estfish_bsot      0.581    0.116    5.022    0.000    0.581    0.631
##    .fish              0.142    0.070    2.024    0.043    0.551    0.551
## 
## R-Square:
##                    Estimate
##     estfish_bsmt      0.309
##     estfish_bsot      0.369
##     fish              0.449
summary(modfitW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 22 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                          9
## 
##                                                   Used       Total
##   Number of observations                           132         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                      15.518
##   Degrees of freedom                                 4
##   P-value (Chi-square)                           0.004
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish_bsmt      1.000                               0.519    0.561
##     estfish_bsot      0.356    0.200    1.777    0.076    0.185    0.226
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish ~                                                                
##     hcope_1           0.231    0.101    2.290    0.022    0.445    0.365
##     amphi_1          -0.283    0.074   -3.829    0.000   -0.545   -0.543
##     clad_1            0.116    0.110    1.055    0.291    0.223    0.183
##     mysid_1          -0.069    0.108   -0.645    0.519   -0.134   -0.103
##     flow_1           -0.140    0.133   -1.048    0.295   -0.269   -0.179
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish_bsmt      0.588    0.198    2.973    0.003    0.588    0.686
##    .estfish_bsot      0.633    0.081    7.783    0.000    0.633    0.949
##    .fish              0.136    0.185    0.736    0.462    0.505    0.505
## 
## R-Square:
##                    Estimate
##     estfish_bsmt      0.314
##     estfish_bsot      0.051
##     fish              0.495
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 28 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                          9
## 
##                                                   Used       Total
##   Number of observations                           126         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                      14.245
##   Degrees of freedom                                 4
##   P-value (Chi-square)                           0.007
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish =~                                                               
##     estfish_bsmt      1.000                               0.344    0.513
##     estfish_bsot      1.614    0.321    5.030    0.000    0.555    0.675
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   fish ~                                                                
##     hcope_1           0.143    0.085    1.668    0.095    0.415    0.218
##     amphi_1           0.052    0.036    1.440    0.150    0.150    0.136
##     clad_1            0.065    0.046    1.426    0.154    0.191    0.149
##     mysid_1          -0.048    0.052   -0.926    0.354   -0.140   -0.110
##     flow_1           -0.479    0.093   -5.125    0.000   -1.392   -0.839
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish_bsmt      0.332    0.047    7.030    0.000    0.332    0.737
##    .estfish_bsot      0.367    0.073    5.001    0.000    0.367    0.544
##    .fish              0.023    0.022    1.051    0.293    0.197    0.197
## 
## R-Square:
##                    Estimate
##     estfish_bsmt      0.263
##     estfish_bsot      0.456
##     fish              0.803
summary(modfitS, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 20 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         13
## 
##                                                   Used       Total
##   Number of observations                           129         312
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                       0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   estfish_bsmt ~                                                        
##     hcope_1          -0.044    0.132   -0.336    0.737   -0.044   -0.032
##     amphi_1           0.113    0.068    1.679    0.093    0.113    0.151
##     clad_1            0.021    0.072    0.296    0.768    0.021    0.026
##     mysid_1           0.160    0.093    1.732    0.083    0.160    0.168
##     flow_1           -0.019    0.082   -0.235    0.814   -0.019   -0.024
##   estfish_bsot ~                                                        
##     hcope_1          -0.189    0.176   -1.074    0.283   -0.189   -0.105
##     amphi_1          -0.059    0.090   -0.653    0.514   -0.059   -0.060
##     clad_1           -0.164    0.095   -1.716    0.086   -0.164   -0.154
##     mysid_1           0.099    0.123    0.800    0.424    0.099    0.078
##     flow_1           -0.013    0.110   -0.118    0.906   -0.013   -0.012
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .estfish_bsmt ~~                                                       
##    .estfish_bsot     -0.017    0.049   -0.338    0.735   -0.017   -0.030
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .estfish_bsmt      0.421    0.052    8.031    0.000    0.421    0.937
##    .estfish_bsot      0.747    0.093    8.031    0.000    0.747    0.962
## 
## R-Square:
##                    Estimate
##     estfish_bsmt      0.063
##     estfish_bsot      0.038
#par(mfrow=c(1,1))
par(mfrow=c(2,2))
semPaths(modfitFW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("Far West", line = 1)
semPaths(modfitW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("West", line = 1)
semPaths(modfitN, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("North", line = 1)
semPaths(modfitS, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("South", line = 1)

Integrated model

South

modS='ammonia~flow_1+chla_1+clad_1
      dophos~flow_1+chla_1+clad_1
      ammonia~~dophos+clad
      chla~chla_1+hcope_1+clad_1+ammonia+dophos+flow_1+corbic_1
      hcope~chla_1+hcope_1+flow_1+pcope_1
      pcope~pcope_1+hcope_1+clad_1
      clad~chla_1+clad_1+flow_1
      corbic~corbic_1+flow_1
'
modfitS=sem(modS, data=filter(fdr_ds,region=="South"))
summary(modfitS, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 36 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         44
## 
##                                                   Used       Total
##   Number of observations                           255         312
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                      47.782
##   Degrees of freedom                                26
##   P-value (Chi-square)                           0.006
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   ammonia ~                                                             
##     flow_1            0.016    0.055    0.298    0.766    0.016    0.019
##     chla_1            0.200    0.060    3.330    0.001    0.200    0.218
##     clad_1           -0.168    0.061   -2.744    0.006   -0.168   -0.181
##   dophos ~                                                              
##     flow_1            0.104    0.072    1.455    0.146    0.104    0.094
##     chla_1           -0.002    0.078   -0.026    0.980   -0.002   -0.002
##     clad_1            0.188    0.079    2.377    0.017    0.188    0.158
##   chla ~                                                                
##     chla_1            0.328    0.059    5.579    0.000    0.328    0.327
##     hcope_1           0.097    0.093    1.041    0.298    0.097    0.062
##     clad_1            0.089    0.060    1.473    0.141    0.089    0.087
##     ammonia          -0.287    0.060   -4.748    0.000   -0.287   -0.263
##     dophos           -0.128    0.046   -2.813    0.005   -0.128   -0.151
##     flow_1           -0.167    0.059   -2.845    0.004   -0.167   -0.177
##     corbic_1          0.062    0.042    1.484    0.138    0.062    0.077
##   hcope ~                                                               
##     chla_1            0.052    0.035    1.472    0.141    0.052    0.084
##     hcope_1           0.326    0.060    5.417    0.000    0.326    0.335
##     flow_1           -0.101    0.036   -2.837    0.005   -0.101   -0.173
##     pcope_1           0.035    0.032    1.072    0.284    0.035    0.059
##   pcope ~                                                               
##     pcope_1           0.458    0.055    8.368    0.000    0.458    0.463
##     hcope_1          -0.133    0.088   -1.502    0.133   -0.133   -0.081
##     clad_1            0.126    0.059    2.126    0.033    0.126    0.119
##   clad ~                                                                
##     chla_1            0.058    0.054    1.069    0.285    0.058    0.059
##     clad_1            0.517    0.055    9.368    0.000    0.517    0.524
##     flow_1            0.030    0.050    0.600    0.548    0.030    0.033
##   corbic ~                                                              
##     corbic_1          0.344    0.056    6.141    0.000    0.344    0.357
##     flow_1            0.110    0.065    1.686    0.092    0.110    0.098
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .ammonia ~~                                                            
##    .dophos            0.149    0.042    3.582    0.000    0.149    0.225
##    .clad             -0.092    0.028   -3.226    0.001   -0.092   -0.197
##  .chla ~~                                                               
##    .hcope             0.043    0.019    2.281    0.023    0.043    0.144
##    .pcope            -0.019    0.031   -0.607    0.544   -0.019   -0.038
##    .clad              0.103    0.028    3.677    0.000    0.103    0.232
##    .corbic           -0.014    0.037   -0.386    0.700   -0.014   -0.024
##  .hcope ~~                                                              
##    .pcope             0.031    0.020    1.549    0.121    0.031    0.097
##    .clad              0.039    0.018    2.202    0.028    0.039    0.136
##    .corbic            0.021    0.024    0.851    0.395    0.021    0.053
##  .pcope ~~                                                              
##    .clad              0.072    0.029    2.474    0.013    0.072    0.154
##    .corbic            0.008    0.040    0.212    0.832    0.008    0.013
##  .clad ~~                                                               
##    .corbic           -0.023    0.035   -0.656    0.512   -0.023   -0.040
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .ammonia           0.512    0.045   11.315    0.000    0.512    0.945
##    .dophos            0.857    0.076   11.292    0.000    0.857    0.961
##    .chla              0.463    0.041   11.292    0.000    0.463    0.719
##    .hcope             0.191    0.017   11.292    0.000    0.191    0.777
##    .pcope             0.520    0.046   11.292    0.000    0.520    0.747
##    .clad              0.425    0.038   11.310    0.000    0.425    0.697
##    .corbic            0.772    0.068   11.292    0.000    0.772    0.854
## 
## R-Square:
##                    Estimate
##     ammonia           0.055
##     dophos            0.039
##     chla              0.281
##     hcope             0.223
##     pcope             0.253
##     clad              0.303
##     corbic            0.146
# semPaths(modfitS, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("South", line = 1)

residuals(modfitS)
## $type
## [1] "raw"
## 
## $cov
##          ammoni dophos chla   hcope  pcope  clad   corbic flow_1 chla_1
## ammonia   0.000                                                        
## dophos    0.000  0.000                                                 
## chla      0.004 -0.003  0.000                                          
## hcope     0.020  0.044 -0.005  0.001                                   
## pcope     0.046  0.180 -0.018  0.003 -0.001                            
## clad      0.006  0.025  0.000  0.003  0.004  0.002                     
## corbic    0.052 -0.142  0.018  0.019 -0.043 -0.003  0.004              
## flow_1    0.000  0.000  0.000  0.000  0.003  0.000  0.000  0.000       
## chla_1    0.000  0.000 -0.003  0.005  0.070  0.009  0.024  0.000  0.000
## clad_1    0.000  0.000  0.001  0.003  0.001  0.001  0.001  0.000  0.000
## hcope_1   0.020  0.015 -0.005  0.001  0.002  0.006 -0.005  0.000  0.000
## corbic_1  0.028 -0.142  0.032  0.065 -0.040  0.014  0.005  0.000  0.000
## pcope_1   0.020  0.132 -0.026 -0.002 -0.001  0.002 -0.067  0.000  0.000
##          clad_1 hcop_1 crbc_1 pcop_1
## ammonia                             
## dophos                              
## chla                                
## hcope                               
## pcope                               
## clad                                
## corbic                              
## flow_1                              
## chla_1                              
## clad_1    0.000                     
## hcope_1   0.000  0.000              
## corbic_1  0.000  0.000  0.000       
## pcope_1   0.000  0.000  0.000  0.000
modificationindices(modfitS)
##          lhs op      rhs     mi    epc sepc.lv sepc.all sepc.nox
## 45    flow_1 ~~   flow_1  0.000  0.000   0.000    0.000    0.000
## 46    flow_1 ~~   chla_1  0.000  0.000   0.000       NA    0.000
## 47    flow_1 ~~   clad_1  0.000  0.000   0.000       NA    0.000
## 48    flow_1 ~~  hcope_1  0.000  0.000   0.000       NA    0.000
## 49    flow_1 ~~ corbic_1  0.000  0.000   0.000       NA    0.000
## 50    flow_1 ~~  pcope_1  0.000  0.000   0.000       NA    0.000
## 51    chla_1 ~~   chla_1  0.000  0.000   0.000    0.000    0.000
## 52    chla_1 ~~   clad_1  0.000  0.000   0.000       NA    0.000
## 53    chla_1 ~~  hcope_1  0.000  0.000   0.000       NA    0.000
## 54    chla_1 ~~ corbic_1  0.000  0.000   0.000       NA    0.000
## 55    chla_1 ~~  pcope_1  0.000  0.000   0.000       NA    0.000
## 56    clad_1 ~~   clad_1  0.000  0.000   0.000    0.000    0.000
## 57    clad_1 ~~  hcope_1  0.000  0.000   0.000       NA    0.000
## 58    clad_1 ~~ corbic_1  0.000  0.000   0.000       NA    0.000
## 59    clad_1 ~~  pcope_1  0.000  0.000   0.000       NA    0.000
## 60   hcope_1 ~~  hcope_1  0.000  0.000   0.000    0.000    0.000
## 61   hcope_1 ~~ corbic_1  0.000  0.000   0.000       NA    0.000
## 62   hcope_1 ~~  pcope_1  0.000  0.000   0.000       NA    0.000
## 63  corbic_1 ~~ corbic_1  0.000  0.000   0.000    0.000    0.000
## 64  corbic_1 ~~  pcope_1  0.000  0.000   0.000       NA    0.000
## 65   pcope_1 ~~  pcope_1  0.000  0.000   0.000    0.000    0.000
## 67   ammonia ~~    hcope  0.073  0.005   0.005    0.016    0.016
## 68   ammonia ~~    pcope  0.009  0.003   0.003    0.006    0.006
## 69   ammonia ~~   corbic  1.939  0.053   0.053    0.085    0.085
## 71    dophos ~~    hcope  1.136  0.026   0.026    0.064    0.064
## 72    dophos ~~    pcope  7.162  0.107   0.107    0.161    0.161
## 73    dophos ~~     clad  0.000 -0.001  -0.001   -0.001   -0.001
## 74    dophos ~~   corbic  4.816 -0.109  -0.109   -0.134   -0.134
## 76   ammonia  ~     chla  0.900  0.309   0.309    0.337    0.337
## 77   ammonia  ~    hcope  0.419  0.061   0.061    0.041    0.041
## 78   ammonia  ~    pcope  0.000  0.001   0.001    0.001    0.001
## 79   ammonia  ~     clad  0.001 -0.008  -0.008   -0.008   -0.008
## 80   ammonia  ~   corbic  3.210  0.083   0.083    0.107    0.107
## 81   ammonia  ~  hcope_1  0.970  0.094   0.094    0.065    0.128
## 82   ammonia  ~ corbic_1  1.720  0.057   0.057    0.077    0.078
## 83   ammonia  ~  pcope_1  0.005 -0.004  -0.004   -0.004   -0.005
## 84    dophos  ~  ammonia  0.000  0.008   0.008    0.006    0.006
## 85    dophos  ~     chla  0.139 -0.091  -0.091   -0.078   -0.078
## 86    dophos  ~    hcope  2.044  0.175   0.175    0.092    0.092
## 87    dophos  ~    pcope 13.860  0.260   0.260    0.229    0.229
## 88    dophos  ~     clad  0.421  0.058   0.058    0.048    0.048
## 89    dophos  ~   corbic  8.690 -0.177  -0.177   -0.178   -0.178
## 90    dophos  ~  hcope_1  0.110  0.042   0.042    0.023    0.044
## 91    dophos  ~ corbic_1  7.505 -0.158  -0.158   -0.165   -0.167
## 92    dophos  ~  pcope_1  7.674  0.194   0.194    0.173    0.205
## 93      chla  ~    hcope  0.018 -0.200  -0.200   -0.124   -0.124
## 94      chla  ~    pcope  0.018 -0.015  -0.015   -0.016   -0.016
## 97      chla  ~  pcope_1  0.018 -0.007  -0.007   -0.007   -0.009
## 98     hcope  ~  ammonia  0.241  0.018   0.018    0.027    0.027
## 99     hcope  ~   dophos  1.366  0.034   0.034    0.064    0.064
## 100    hcope  ~     chla  0.089 -0.029  -0.029   -0.047   -0.047
## 101    hcope  ~    pcope  0.030  0.055   0.055    0.092    0.092
## 102    hcope  ~     clad  0.005  0.005   0.005    0.008    0.008
## 103    hcope  ~   corbic  5.977  0.198   0.198    0.379    0.379
## 104    hcope  ~   clad_1  0.030  0.007   0.007    0.011    0.014
## 105    hcope  ~ corbic_1  5.977  0.068   0.068    0.135    0.137
## 106    pcope  ~  ammonia  1.274  0.069   0.069    0.061    0.061
## 107    pcope  ~   dophos  7.626  0.132   0.132    0.150    0.150
## 108    pcope  ~     chla  0.166 -0.050  -0.050   -0.048   -0.048
## 109    pcope  ~    hcope  0.669  0.412   0.412    0.245    0.245
## 110    pcope  ~     clad  0.318  0.182   0.182    0.170    0.170
## 111    pcope  ~   corbic  1.200 -0.135  -0.135   -0.154   -0.154
## 112    pcope  ~   flow_1  0.008  0.005   0.005    0.006    0.006
## 113    pcope  ~   chla_1  4.332  0.127   0.127    0.122    0.152
## 114    pcope  ~ corbic_1  1.410 -0.054  -0.054   -0.063   -0.064
## 115     clad  ~  ammonia  0.000 -0.005  -0.005   -0.005   -0.005
## 116     clad  ~   dophos  0.000 -0.001  -0.001   -0.001   -0.001
## 117     clad  ~     chla  0.086  0.064   0.064    0.066    0.066
## 118     clad  ~    hcope  0.230  0.126   0.126    0.080    0.080
## 119     clad  ~    pcope  0.006 -0.008  -0.008   -0.009   -0.009
## 120     clad  ~   corbic  0.214  0.054   0.054    0.065    0.065
## 121     clad  ~  hcope_1  0.247  0.044   0.044    0.029    0.057
## 122     clad  ~ corbic_1  0.214  0.018   0.018    0.023    0.024
## 123     clad  ~  pcope_1  0.000  0.000   0.000    0.000    0.000
## 124   corbic  ~  ammonia  1.017  0.075   0.075    0.058    0.058
## 125   corbic  ~   dophos  3.633 -0.112  -0.112   -0.111   -0.111
## 126   corbic  ~     chla  0.155  0.058   0.058    0.049    0.049
## 127   corbic  ~    hcope  0.080 -0.093  -0.093   -0.048   -0.048
## 128   corbic  ~    pcope  1.751 -0.175  -0.175   -0.154   -0.154
## 129   corbic  ~     clad  0.123 -0.044  -0.044   -0.036   -0.036
## 130   corbic  ~   chla_1  0.284  0.037   0.037    0.031    0.039
## 131   corbic  ~   clad_1  0.000  0.001   0.001    0.001    0.001
## 132   corbic  ~  hcope_1  0.032 -0.022  -0.022   -0.012   -0.023
## 133   corbic  ~  pcope_1  2.122 -0.095  -0.095   -0.084   -0.100
## 134   flow_1  ~  ammonia  0.512  0.093   0.093    0.081    0.081
## 135   flow_1  ~   dophos  2.043  0.146   0.146    0.162    0.162
## 136   flow_1  ~     chla  1.038 -0.247  -0.247   -0.233   -0.233
## 137   flow_1  ~    hcope  0.679 -0.189  -0.189   -0.110   -0.110
## 138   flow_1  ~    pcope  0.199  0.021   0.021    0.020    0.020
## 139   flow_1  ~     clad  0.035  0.018   0.018    0.016    0.016
## 140   flow_1  ~   corbic  0.002 -0.004  -0.004   -0.004   -0.004
## 141   flow_1  ~   chla_1  0.000  0.000   0.000    0.000    0.000
## 142   flow_1  ~   clad_1  0.000  0.000   0.000    0.000    0.000
## 143   flow_1  ~  hcope_1  0.000  0.000   0.000    0.000    0.000
## 144   flow_1  ~ corbic_1  0.000  0.000   0.000    0.000    0.000
## 145   flow_1  ~  pcope_1  0.000  0.000   0.000    0.000    0.000
## 146   chla_1  ~  ammonia  0.057 -0.071  -0.071   -0.065   -0.065
## 147   chla_1  ~   dophos  2.697  0.382   0.382    0.451    0.451
## 148   chla_1  ~     chla  0.567 -0.212  -0.212   -0.213   -0.213
## 149   chla_1  ~    hcope  0.126  0.083   0.083    0.051    0.051
## 150   chla_1  ~    pcope  2.424  0.073   0.073    0.076    0.076
## 151   chla_1  ~     clad  0.377  0.102   0.102    0.099    0.099
## 152   chla_1  ~   corbic  0.112  0.015   0.015    0.018    0.018
## 153   chla_1  ~   flow_1  0.000  0.000   0.000    0.000    0.000
## 154   chla_1  ~   clad_1  0.000  0.000   0.000    0.000    0.000
## 155   chla_1  ~  hcope_1  0.000  0.000   0.000    0.000    0.000
## 156   chla_1  ~ corbic_1  0.000  0.000   0.000    0.000    0.000
## 157   chla_1  ~  pcope_1  0.000  0.000   0.000    0.000    0.000
## 158   clad_1  ~  ammonia  0.666 -0.145  -0.145   -0.135   -0.135
## 159   clad_1  ~   dophos  7.196 -0.374  -0.374   -0.448   -0.448
## 160   clad_1  ~     chla  0.414  0.083   0.083    0.084    0.084
## 161   clad_1  ~    hcope  0.016  0.010   0.010    0.006    0.006
## 162   clad_1  ~    pcope  1.438 -0.118  -0.118   -0.125   -0.125
## 163   clad_1  ~     clad  0.063 -0.033  -0.033   -0.033   -0.033
## 164   clad_1  ~   corbic  0.051  0.009   0.009    0.011    0.011
## 165   clad_1  ~   flow_1  0.000  0.000   0.000    0.000    0.000
## 166   clad_1  ~   chla_1  0.000  0.000   0.000    0.000    0.000
## 167   clad_1  ~  hcope_1  0.000  0.000   0.000    0.000    0.000
## 168   clad_1  ~ corbic_1  0.000  0.000   0.000    0.000    0.000
## 169   clad_1  ~  pcope_1  0.000  0.000   0.000    0.000    0.000
## 170  hcope_1  ~  ammonia  0.696  0.030   0.030    0.044    0.044
## 171  hcope_1  ~   dophos  0.350  0.017   0.017    0.031    0.031
## 172  hcope_1  ~     chla  0.319 -0.044  -0.044   -0.068   -0.068
## 173  hcope_1  ~    hcope  0.866 -0.195  -0.195   -0.190   -0.190
## 174  hcope_1  ~    pcope  0.007 -0.004  -0.004   -0.007   -0.007
## 175  hcope_1  ~     clad  0.025  0.005   0.005    0.007    0.007
## 176  hcope_1  ~   corbic  0.018 -0.004  -0.004   -0.007   -0.007
## 177  hcope_1  ~   flow_1  0.000  0.000   0.000    0.000    0.000
## 178  hcope_1  ~   chla_1  0.000  0.000   0.000    0.000    0.000
## 179  hcope_1  ~   clad_1  0.000  0.000   0.000    0.000    0.000
## 180  hcope_1  ~ corbic_1  0.000  0.000   0.000    0.000    0.000
## 181  hcope_1  ~  pcope_1  0.000  0.000   0.000    0.000    0.000
## 182 corbic_1  ~  ammonia  0.247  0.040   0.040    0.030    0.030
## 183 corbic_1  ~   dophos  5.964 -0.153  -0.153   -0.146   -0.146
## 184 corbic_1  ~     chla  1.622  0.184   0.184    0.150    0.150
## 185 corbic_1  ~    hcope  3.474  0.200   0.200    0.101    0.101
## 186 corbic_1  ~    pcope  0.461 -0.042  -0.042   -0.035   -0.035
## 187 corbic_1  ~     clad  0.048  0.014   0.014    0.011    0.011
## 188 corbic_1  ~   corbic  0.505  0.324   0.324    0.312    0.312
## 189 corbic_1  ~   flow_1  0.000  0.000   0.000    0.000    0.000
## 190 corbic_1  ~   chla_1  0.000  0.000   0.000    0.000    0.000
## 191 corbic_1  ~   clad_1  0.000  0.000   0.000    0.000    0.000
## 192 corbic_1  ~  hcope_1  0.000  0.000   0.000    0.000    0.000
## 193 corbic_1  ~  pcope_1  0.000  0.000   0.000    0.000    0.000
## 194  pcope_1  ~  ammonia  0.199  0.029   0.029    0.026    0.026
## 195  pcope_1  ~   dophos  6.920  0.136   0.136    0.152    0.152
## 196  pcope_1  ~     chla  0.324 -0.032  -0.032   -0.030   -0.030
## 197  pcope_1  ~    hcope  0.035 -0.042  -0.042   -0.025   -0.025
## 198  pcope_1  ~    pcope  0.510  0.124   0.124    0.123    0.123
## 199  pcope_1  ~     clad  0.002  0.002   0.002    0.002    0.002
## 200  pcope_1  ~   corbic  1.426 -0.057  -0.057   -0.065   -0.065
## 201  pcope_1  ~   flow_1  0.000  0.000   0.000    0.000    0.000
## 202  pcope_1  ~   chla_1  0.000  0.000   0.000    0.000    0.000
## 203  pcope_1  ~   clad_1  0.000  0.000   0.000    0.000    0.000
## 204  pcope_1  ~  hcope_1  0.000  0.000   0.000    0.000    0.000
## 205  pcope_1  ~ corbic_1  0.000  0.000   0.000    0.000    0.000
labelssouth=createLabels(modfitS, cnameslag)

myLavaanPlot(model=modfitS, labels=labelssouth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))

North

modN='ammonia~flow_1+chla_1+clad_1
      dophos~flow_1+chla_1+clad_1
      ammonia~~dophos+clad
      dophos~~clad
      chla~chla_1+hcope_1+clad_1+ammonia+dophos+flow_1+corbic_1
      hcope~chla_1+hcope_1+flow_1
      clad~chla_1+clad_1+flow_1
      corbic~corbic_1+flow_1
'
modfitN=sem(modN, data=filter(fdr_ds,region=="North"))
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 33 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         36
## 
##                                                   Used       Total
##   Number of observations                           254         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                      21.390
##   Degrees of freedom                                15
##   P-value (Chi-square)                           0.125
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   ammonia ~                                                             
##     flow_1           -0.232    0.055   -4.181    0.000   -0.232   -0.263
##     chla_1            0.042    0.053    0.804    0.421    0.042    0.048
##     clad_1           -0.084    0.060   -1.392    0.164   -0.084   -0.088
##   dophos ~                                                              
##     flow_1           -0.294    0.063   -4.677    0.000   -0.294   -0.269
##     chla_1           -0.050    0.060   -0.842    0.400   -0.050   -0.046
##     clad_1           -0.382    0.068   -5.615    0.000   -0.382   -0.324
##   chla ~                                                                
##     chla_1            0.163    0.060    2.733    0.006    0.163    0.163
##     hcope_1          -0.029    0.101   -0.285    0.776   -0.029   -0.020
##     clad_1            0.090    0.071    1.262    0.207    0.090    0.083
##     ammonia          -0.309    0.070   -4.399    0.000   -0.309   -0.272
##     dophos           -0.060    0.062   -0.971    0.331   -0.060   -0.065
##     flow_1           -0.106    0.074   -1.425    0.154   -0.106   -0.105
##     corbic_1         -0.067    0.050   -1.341    0.180   -0.067   -0.079
##   hcope ~                                                               
##     chla_1            0.005    0.037    0.133    0.894    0.005    0.008
##     hcope_1           0.202    0.062    3.249    0.001    0.202    0.215
##     flow_1           -0.185    0.044   -4.219    0.000   -0.185   -0.282
##   clad ~                                                                
##     chla_1           -0.071    0.050   -1.412    0.158   -0.071   -0.077
##     clad_1            0.431    0.056    7.642    0.000    0.431    0.431
##     flow_1            0.153    0.053    2.907    0.004    0.153    0.165
##   corbic ~                                                              
##     corbic_1          0.494    0.054    9.061    0.000    0.494    0.493
##     flow_1            0.059    0.064    0.919    0.358    0.059    0.050
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .ammonia ~~                                                            
##    .dophos            0.097    0.035    2.795    0.005    0.097    0.178
##    .clad             -0.022    0.028   -0.783    0.433   -0.022   -0.049
##  .dophos ~~                                                             
##    .clad             -0.125    0.033   -3.787    0.000   -0.125   -0.242
##  .chla ~~                                                               
##    .hcope             0.044    0.024    1.851    0.064    0.044    0.117
##    .clad              0.020    0.031    0.634    0.526    0.020    0.039
##    .corbic           -0.072    0.041   -1.740    0.082   -0.072   -0.110
##  .hcope ~~                                                              
##    .clad             -0.050    0.020   -2.534    0.011   -0.050   -0.156
##    .corbic           -0.017    0.026   -0.662    0.508   -0.017   -0.042
##  .clad ~~                                                               
##    .corbic            0.002    0.034    0.074    0.941    0.002    0.004
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .ammonia           0.480    0.043   11.269    0.000    0.480    0.909
##    .dophos            0.616    0.055   11.269    0.000    0.616    0.762
##    .chla              0.596    0.053   11.269    0.000    0.596    0.874
##    .hcope             0.237    0.021   11.269    0.000    0.237    0.810
##    .clad              0.434    0.038   11.286    0.000    0.434    0.745
##    .corbic            0.714    0.063   11.269    0.000    0.714    0.754
## 
## R-Square:
##                    Estimate
##     ammonia           0.091
##     dophos            0.238
##     chla              0.126
##     hcope             0.190
##     clad              0.255
##     corbic            0.246
# semPaths(modfitS, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("South", line = 1)

residuals(modfitN)
## $type
## [1] "raw"
## 
## $cov
##          ammoni dophos chla   hcope  clad   corbic flow_1 chla_1 clad_1
## ammonia   0.000                                                        
## dophos    0.000  0.000                                                 
## chla      0.001  0.020 -0.006                                          
## hcope     0.025  0.050 -0.012  0.002                                   
## clad     -0.005 -0.011 -0.003 -0.022  0.006                            
## corbic    0.023 -0.085  0.010  0.000  0.039 -0.001                     
## flow_1    0.000  0.000  0.000  0.000  0.000  0.000  0.000              
## chla_1    0.000  0.000 -0.003 -0.001  0.000  0.029  0.000  0.000       
## clad_1    0.000  0.000 -0.007 -0.007  0.001  0.061  0.000  0.000  0.000
## hcope_1   0.004 -0.016  0.000  0.006 -0.043 -0.021  0.000  0.000  0.000
## corbic_1  0.046 -0.090 -0.007  0.008  0.024 -0.001  0.000  0.000  0.000
##          hcop_1 crbc_1
## ammonia               
## dophos                
## chla                  
## hcope                 
## clad                  
## corbic                
## flow_1                
## chla_1                
## clad_1                
## hcope_1   0.000       
## corbic_1  0.000  0.000
modificationindices(modfitN)
##          lhs op      rhs    mi    epc sepc.lv sepc.all sepc.nox
## 37    flow_1 ~~   flow_1 0.000  0.000   0.000    0.000    0.000
## 38    flow_1 ~~   chla_1 0.000  0.000   0.000       NA    0.000
## 39    flow_1 ~~   clad_1 0.000  0.000   0.000       NA    0.000
## 40    flow_1 ~~  hcope_1 0.000  0.000   0.000       NA    0.000
## 41    flow_1 ~~ corbic_1 0.000  0.000   0.000       NA    0.000
## 42    chla_1 ~~   chla_1 0.000  0.000   0.000    0.000    0.000
## 43    chla_1 ~~   clad_1 0.000  0.000   0.000       NA    0.000
## 44    chla_1 ~~  hcope_1 0.000  0.000   0.000       NA    0.000
## 45    chla_1 ~~ corbic_1 0.000  0.000   0.000       NA    0.000
## 46    clad_1 ~~   clad_1 0.000  0.000   0.000    0.000    0.000
## 47    clad_1 ~~  hcope_1 0.000  0.000   0.000       NA    0.000
## 48    clad_1 ~~ corbic_1 0.000  0.000   0.000       NA    0.000
## 49   hcope_1 ~~  hcope_1 0.000  0.000   0.000    0.000    0.000
## 50   hcope_1 ~~ corbic_1 0.000  0.000   0.000       NA    0.000
## 51  corbic_1 ~~ corbic_1 0.000  0.000   0.000    0.000    0.000
## 53   ammonia ~~    hcope 0.568  0.016   0.016    0.047    0.047
## 54   ammonia ~~   corbic 0.044  0.008   0.008    0.013    0.013
## 56    dophos ~~    hcope 3.749  0.046   0.046    0.119    0.119
## 57    dophos ~~   corbic 0.102 -0.013  -0.013   -0.020   -0.020
## 59   ammonia  ~     chla 0.126 -0.110  -0.110   -0.125   -0.125
## 60   ammonia  ~    hcope 0.629  0.068   0.068    0.051    0.051
## 61   ammonia  ~     clad 0.560 -0.311  -0.311   -0.327   -0.327
## 62   ammonia  ~   corbic 0.769  0.039   0.039    0.052    0.052
## 63   ammonia  ~  hcope_1 0.099  0.028   0.028    0.022    0.039
## 64   ammonia  ~ corbic_1 2.172  0.066   0.066    0.088    0.090
## 66    dophos  ~     chla 6.531  0.892   0.892    0.819    0.819
## 67    dophos  ~    hcope 2.744  0.161   0.161    0.097    0.097
## 68    dophos  ~     clad 3.791 -0.917  -0.917   -0.778   -0.778
## 69    dophos  ~   corbic 1.857 -0.068  -0.068   -0.073   -0.073
## 70    dophos  ~  hcope_1 1.640 -0.127  -0.127   -0.081   -0.141
## 71    dophos  ~ corbic_1 4.120 -0.099  -0.099   -0.107   -0.111
## 75     hcope  ~  ammonia 1.289  0.050   0.050    0.067    0.067
## 76     hcope  ~   dophos 4.309  0.076   0.076    0.127    0.127
## 77     hcope  ~     chla 2.285 -0.182  -0.182   -0.278   -0.278
## 78     hcope  ~     clad 1.474 -0.107  -0.107   -0.151   -0.151
## 79     hcope  ~   corbic 0.084  0.018   0.018    0.033    0.033
## 80     hcope  ~   clad_1 0.072 -0.011  -0.011   -0.016   -0.021
## 81     hcope  ~ corbic_1 0.084  0.009   0.009    0.016    0.017
## 84      clad  ~     chla 0.086  0.177   0.177    0.192    0.192
## 85      clad  ~    hcope 5.875 -1.008  -1.008   -0.716   -0.716
## 86      clad  ~   corbic 0.045  0.018   0.018    0.022    0.022
## 87      clad  ~  hcope_1 5.875 -0.204  -0.204   -0.154   -0.267
## 88      clad  ~ corbic_1 0.045  0.009   0.009    0.011    0.011
## 89    corbic  ~  ammonia 0.003  0.004   0.004    0.003    0.003
## 90    corbic  ~   dophos 0.679 -0.052  -0.052   -0.048   -0.048
## 91    corbic  ~     chla 0.676  0.154   0.154    0.131    0.131
## 92    corbic  ~    hcope 0.599 -0.413  -0.413   -0.230   -0.230
## 93    corbic  ~     clad 2.025  0.215   0.215    0.169    0.169
## 94    corbic  ~   chla_1 0.435  0.042   0.042    0.036    0.044
## 95    corbic  ~   clad_1 2.482  0.115   0.115    0.090    0.119
## 96    corbic  ~  hcope_1 0.649 -0.088  -0.088   -0.052   -0.090
## 97    flow_1  ~  ammonia 0.052  0.025   0.025    0.022    0.022
## 98    flow_1  ~   dophos 0.314 -0.045  -0.045   -0.050   -0.050
## 99    flow_1  ~     chla 0.021  0.041   0.041    0.041    0.041
## 100   flow_1  ~    hcope 0.919  0.294   0.294    0.193    0.193
## 101   flow_1  ~     clad 2.438 -0.148  -0.148   -0.137   -0.137
## 102   flow_1  ~   corbic 0.914 -0.062  -0.062   -0.073   -0.073
## 103   flow_1  ~   chla_1 0.000  0.000   0.000    0.000    0.000
## 104   flow_1  ~   clad_1 0.000  0.000   0.000    0.000    0.000
## 105   flow_1  ~  hcope_1 0.000  0.000   0.000    0.000    0.000
## 106   flow_1  ~ corbic_1 0.000  0.000   0.000    0.000    0.000
## 107   chla_1  ~  ammonia 0.232  0.168   0.168    0.147    0.147
## 108   chla_1  ~   dophos 0.254 -0.128  -0.128   -0.139   -0.139
## 109   chla_1  ~     chla 0.132 -0.139  -0.139   -0.138   -0.138
## 110   chla_1  ~    hcope 0.006 -0.036  -0.036   -0.024   -0.024
## 111   chla_1  ~     clad 2.073  0.421   0.421    0.387    0.387
## 112   chla_1  ~   corbic 0.197  0.020   0.020    0.024    0.024
## 113   chla_1  ~   flow_1 0.000  0.000   0.000    0.000    0.000
## 114   chla_1  ~   clad_1 0.000  0.000   0.000    0.000    0.000
## 115   chla_1  ~  hcope_1 0.000  0.000   0.000    0.000    0.000
## 116   chla_1  ~ corbic_1 0.000  0.000   0.000    0.000    0.000
## 117   clad_1  ~  ammonia 0.070 -0.062  -0.062   -0.059   -0.059
## 118   clad_1  ~   dophos 0.035  0.031   0.031    0.036    0.036
## 119   clad_1  ~     chla 0.854 -0.260  -0.260   -0.282   -0.282
## 120   clad_1  ~    hcope 0.052 -0.017  -0.017   -0.012   -0.012
## 121   clad_1  ~     clad 0.885 -0.147  -0.147   -0.147   -0.147
## 122   clad_1  ~   corbic 1.042  0.039   0.039    0.050    0.050
## 123   clad_1  ~   flow_1 0.000  0.000   0.000    0.000    0.000
## 124   clad_1  ~   chla_1 0.000  0.000   0.000    0.000    0.000
## 125   clad_1  ~  hcope_1 0.000  0.000   0.000    0.000    0.000
## 126   clad_1  ~ corbic_1 0.000  0.000   0.000    0.000    0.000
## 127  hcope_1  ~  ammonia 0.044  0.008   0.008    0.010    0.010
## 128  hcope_1  ~   dophos 0.297 -0.015  -0.015   -0.024   -0.024
## 129  hcope_1  ~     chla 0.018 -0.014  -0.014   -0.020   -0.020
## 130  hcope_1  ~    hcope 0.755  0.123   0.123    0.116    0.116
## 131  hcope_1  ~     clad 2.512 -0.053  -0.053   -0.070   -0.070
## 132  hcope_1  ~   corbic 0.231 -0.013  -0.013   -0.022   -0.022
## 133  hcope_1  ~   flow_1 0.000  0.000   0.000    0.000    0.000
## 134  hcope_1  ~   chla_1 0.000  0.000   0.000    0.000    0.000
## 135  hcope_1  ~   clad_1 0.000  0.000   0.000    0.000    0.000
## 136  hcope_1  ~ corbic_1 0.000  0.000   0.000    0.000    0.000
## 137 corbic_1  ~  ammonia 0.979  0.076   0.076    0.057    0.057
## 138 corbic_1  ~   dophos 1.972 -0.078  -0.078   -0.073   -0.073
## 139 corbic_1  ~     chla 0.125 -0.072  -0.072   -0.061   -0.061
## 140 corbic_1  ~    hcope 0.056  0.023   0.023    0.013    0.013
## 141 corbic_1  ~     clad 0.154  0.025   0.025    0.020    0.020
## 142 corbic_1  ~   corbic 0.243 -0.152  -0.152   -0.153   -0.153
## 143 corbic_1  ~   flow_1 0.000  0.000   0.000    0.000    0.000
## 144 corbic_1  ~   chla_1 0.000  0.000   0.000    0.000    0.000
## 145 corbic_1  ~   clad_1 0.000  0.000   0.000    0.000    0.000
## 146 corbic_1  ~  hcope_1 0.000  0.000   0.000    0.000    0.000
labelsnorth=createLabels(modfitN, cnameslag)

myLavaanPlot(model=modfitN, labels=labelsnorth,
                         node_options=list(shape="box", fontname="Helvetica"), 
                         coefs=TRUE, stand=TRUE, covs=FALSE, sig=0.05, 
                         width=c("regress","latent"),
                         color=c("regress","latent"))
modFW='chla~chla_1+hcope_1+potam_1+amphi_1
       hcope~chla_1+hcope_1
       potam~chla_1+potam_1
       amphi~chla_1+amphi_1
'
modWN='chla~chla_1+hcope_1+potam_1+amphi_1+clad_1
       hcope~chla_1+hcope_1
       potam~chla_1+potam_1
       amphi~chla_1+amphi_1
       clad~chla_1+clad_1
'



modfitFW=sem(modFW, data=filter(fdr_ds,region=="Far West"))
modfitW=sem(modWN, data=filter(fdr_ds,region=="West"))
modfitN=sem(modWN, data=filter(fdr_ds,region=="North"))
summary(modfitFW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 25 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         20
## 
##                                                   Used       Total
##   Number of observations                           234         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                      18.231
##   Degrees of freedom                                 6
##   P-value (Chi-square)                           0.006
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.237    0.063    3.753    0.000    0.237    0.239
##     hcope_1           0.056    0.054    1.037    0.300    0.056    0.067
##     potam_1          -0.014    0.045   -0.303    0.762   -0.014   -0.019
##     amphi_1           0.076    0.048    1.595    0.111    0.076    0.101
##   hcope ~                                                               
##     chla_1            0.053    0.082    0.645    0.519    0.053    0.039
##     hcope_1           0.467    0.069    6.759    0.000    0.467    0.405
##   potam ~                                                               
##     chla_1            0.073    0.069    1.056    0.291    0.073    0.051
##     potam_1           0.672    0.049   13.846    0.000    0.672    0.668
##   amphi ~                                                               
##     chla_1            0.042    0.044    0.960    0.337    0.042    0.032
##     amphi_1           0.846    0.033   25.440    0.000    0.846    0.855
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.028    0.037    0.741    0.458    0.028    0.049
##    .potam             0.031    0.032    0.999    0.318    0.031    0.065
##    .amphi            -0.003    0.020   -0.170    0.865   -0.003   -0.011
##  .hcope ~~                                                              
##    .potam             0.050    0.041    1.219    0.223    0.050    0.080
##    .amphi             0.012    0.026    0.471    0.637    0.012    0.031
##  .potam ~~                                                              
##    .amphi             0.017    0.022    0.766    0.444    0.017    0.050
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.434    0.040   10.817    0.000    0.434    0.916
##    .hcope             0.747    0.069   10.817    0.000    0.747    0.830
##    .potam             0.533    0.049   10.817    0.000    0.533    0.550
##    .amphi             0.215    0.020   10.817    0.000    0.215    0.262
## 
## R-Square:
##                    Estimate
##     chla              0.084
##     hcope             0.170
##     potam             0.450
##     amphi             0.738
summary(modfitW, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 33 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         28
## 
##                                                   Used       Total
##   Number of observations                           257         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                      29.989
##   Degrees of freedom                                12
##   P-value (Chi-square)                           0.003
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.183    0.060    3.036    0.002    0.183    0.185
##     hcope_1           0.072    0.050    1.420    0.156    0.072    0.088
##     potam_1          -0.004    0.050   -0.071    0.943   -0.004   -0.005
##     amphi_1           0.055    0.044    1.249    0.212    0.055    0.079
##     clad_1            0.220    0.054    4.048    0.000    0.220    0.246
##   hcope ~                                                               
##     chla_1            0.067    0.072    0.932    0.351    0.067    0.056
##     hcope_1           0.350    0.058    5.998    0.000    0.350    0.353
##   potam ~                                                               
##     chla_1           -0.027    0.052   -0.520    0.603   -0.027   -0.020
##     potam_1           0.772    0.038   20.105    0.000    0.772    0.775
##   amphi ~                                                               
##     chla_1            0.000    0.046    0.007    0.995    0.000    0.000
##     amphi_1           0.850    0.032   26.321    0.000    0.850    0.853
##   clad ~                                                                
##     chla_1            0.046    0.062    0.731    0.464    0.046    0.041
##     clad_1            0.444    0.055    8.090    0.000    0.444    0.444
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.076    0.031    2.459    0.014    0.076    0.155
##    .potam             0.002    0.023    0.102    0.919    0.002    0.006
##    .amphi             0.014    0.020    0.679    0.497    0.014    0.042
##    .clad              0.004    0.027    0.134    0.893    0.004    0.008
##  .hcope ~~                                                              
##    .potam            -0.037    0.028   -1.324    0.185   -0.037   -0.083
##    .amphi            -0.024    0.025   -0.957    0.339   -0.024   -0.060
##    .clad             -0.064    0.033   -1.932    0.053   -0.064   -0.121
##  .potam ~~                                                              
##    .amphi             0.027    0.018    1.472    0.141    0.027    0.092
##    .clad             -0.067    0.025   -2.712    0.007   -0.067   -0.172
##  .amphi ~~                                                              
##    .clad              0.003    0.022    0.151    0.880    0.003    0.009
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.406    0.036   11.336    0.000    0.406    0.874
##    .hcope             0.596    0.053   11.336    0.000    0.596    0.863
##    .potam             0.332    0.029   11.336    0.000    0.332    0.397
##    .amphi             0.260    0.023   11.336    0.000    0.260    0.272
##    .clad              0.465    0.041   11.336    0.000    0.465    0.797
## 
## R-Square:
##                    Estimate
##     chla              0.126
##     hcope             0.137
##     potam             0.603
##     amphi             0.728
##     clad              0.203
summary(modfitN, standardized=T, rsq=T)
## lavaan 0.6-4 ended normally after 31 iterations
## 
##   Optimization method                           NLMINB
##   Number of free parameters                         28
## 
##                                                   Used       Total
##   Number of observations                           255         311
## 
##   Estimator                                         ML
##   Model Fit Test Statistic                      60.092
##   Degrees of freedom                                12
##   P-value (Chi-square)                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Information saturated (h1) model          Structured
##   Standard Errors                             Standard
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##   chla ~                                                                
##     chla_1            0.153    0.061    2.488    0.013    0.153    0.154
##     hcope_1           0.016    0.092    0.176    0.860    0.016    0.011
##     potam_1          -0.072    0.068   -1.054    0.292   -0.072   -0.067
##     amphi_1           0.008    0.057    0.138    0.890    0.008    0.009
##     clad_1            0.130    0.072    1.811    0.070    0.130    0.121
##   hcope ~                                                               
##     chla_1           -0.013    0.038   -0.342    0.732   -0.013   -0.020
##     hcope_1           0.324    0.053    6.063    0.000    0.324    0.349
##   potam ~                                                               
##     chla_1           -0.048    0.048   -1.001    0.317   -0.048   -0.052
##     potam_1           0.542    0.050   10.900    0.000    0.542    0.554
##   amphi ~                                                               
##     chla_1            0.082    0.059    1.381    0.167    0.082    0.074
##     amphi_1           0.507    0.054    9.365    0.000    0.507    0.504
##   clad ~                                                                
##     chla_1           -0.061    0.051   -1.197    0.231   -0.061   -0.068
##     clad_1            0.432    0.053    8.087    0.000    0.432    0.441
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##  .chla ~~                                                               
##    .hcope             0.034    0.025    1.326    0.185    0.034    0.083
##    .potam             0.043    0.032    1.346    0.178    0.043    0.085
##    .amphi            -0.037    0.039   -0.945    0.345   -0.037   -0.059
##    .clad              0.034    0.034    0.997    0.319    0.034    0.063
##  .hcope ~~                                                              
##    .potam             0.036    0.020    1.791    0.073    0.036    0.113
##    .amphi             0.013    0.025    0.548    0.584    0.013    0.034
##    .clad             -0.069    0.022   -3.211    0.001   -0.069   -0.205
##  .potam ~~                                                              
##    .amphi             0.003    0.031    0.111    0.911    0.003    0.007
##    .clad             -0.085    0.027   -3.142    0.002   -0.085   -0.201
##  .amphi ~~                                                              
##    .clad              0.028    0.033    0.860    0.390    0.028    0.054
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)   Std.lv  Std.all
##    .chla              0.645    0.057   11.292    0.000    0.645    0.949
##    .hcope             0.252    0.022   11.292    0.000    0.252    0.879
##    .potam             0.396    0.035   11.292    0.000    0.396    0.689
##    .amphi             0.612    0.054   11.292    0.000    0.612    0.742
##    .clad              0.452    0.040   11.292    0.000    0.452    0.807
## 
## R-Square:
##                    Estimate
##     chla              0.051
##     hcope             0.121
##     potam             0.311
##     amphi             0.258
##     clad              0.193
#par(mfrow=c(1,1))
par(mfrow=c(2,2))
semPaths(modfitFW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("Far West", line = 1)
semPaths(modfitW, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("West", line = 1)
semPaths(modfitN, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("North", line = 1)
semPaths(modfitS, "std", edge.label.cex = 1, node.width=2, nCharNodes = 0, residuals = F, intercepts = F); title("South", line = 1)

Nice plots

These need to be updated.

Without covariances

Far West

West

North

South

With covariances